﻿using BLL;
using Models;
using Inspinia_MVC5.Code;
using Inspinia_MVC5.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.Mvc;
using DataTables.Mvc;

namespace Inspinia_MVC5.Controllers
{
    [AllowCors]
    [SessionTimeOutFilter]
    public class WarnLogController : Controller
    {
        // GET: WarnLof
        public JsonResult Index(string blockid, int page = 1, int size = 16, string sortkey = null, string search = null)
        {
            int num = 5;
            string blockwellstr = "";
            WellService WellService = new WellService();
            var wells = WellService.All();
            List<WarnLogModel> WarnLogModel = new List<WarnLogModel>();
            if (Session["identity"].ToString() == null)
            {
                return Json(new { status = "7" }, JsonRequestBehavior.AllowGet);
            }
            else if (Session["identity"].ToString() == "Admin")
            {
                if (blockid != "0")
                {
                    wells = wells.Where(c => c.BLOCK_ID.ToString() == blockid);
                }
            }
            else
            {
                if (blockid == "0")
                {
                    string wellstr = Session["wellstr"].ToString();
                    wells = wells.Where(c => wellstr.Contains("-" + c.ID.ToString() + "-"));
                }
                else
                {
                    blockwellstr = getBlockWell(blockid);
                    wells = wells.Where(c => blockwellstr.Contains("-" + c.ID.ToString() + "-"));
                }
            }
            foreach (var well in wells)
            {
                List<WarnLogModel> inddmodel = new List<WarnLogModel>();
                inddmodel = ResultHelper.getWarnsByNum(well.ID.ToString(), num);
                WarnLogModel = WarnLogModel.Union(inddmodel).ToList();
            }
            if (WarnLogModel.Count() == 0)
                return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);

            if (!string.IsNullOrEmpty(search))
            {
                WarnLogModel = WarnLogModel.Where(u => u.isCheck.Contains(search) || u.IDCode.Contains(search) || u.WELL_ID.Contains(search)).ToList();
            }
            WarnLogModel = sortWarnLogModel(sortkey, WarnLogModel);
            return Json(new { status = "0", totalnum = WarnLogModel.Count(), data = WarnLogModel.Skip((page - 1) * size).Take(size) }, JsonRequestBehavior.AllowGet);
        }
        // GET: WarnLof
        public JsonResult WellWarn(string wellid, int page = 1, int size = 16, string sortkey = null, string search = null)
        {
            string wellstr = Session["wellstr"].ToString();
            if (wellstr == "0" || wellstr.Contains("-" + wellid + "-"))
            {
                List<WarnLogModel> WarnLogModel = new List<WarnLogModel>();
                WarnLogModel = ResultHelper.getWarnsByWellid(wellid);
                if (WarnLogModel.Count() == 0)
                    return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
                if (!string.IsNullOrEmpty(search))
                {
                    WarnLogModel = WarnLogModel.Where(u => u.isCheck.Contains(search) || u.IDCode.Contains(search)).ToList();
                }
                WarnLogModel = sortWarnLogModel(sortkey, WarnLogModel);
                return Json(new { status = "0", totalnum = WarnLogModel.Count(), data = WarnLogModel.Skip((page - 1) * size).Take(size) }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "2" }, JsonRequestBehavior.AllowGet);
            }
        }
        public JsonResult WellOperation(string wellid, int page = 1, int size = 16, string sortkey = null, string search = null)
        {
            string wellstr = Session["wellstr"].ToString();
            if (wellstr == "0" || wellstr.Contains("-" + wellid + "-"))
            {
                List<WarnLogModel> WarnLogModel = new List<WarnLogModel>();
                WarnLogModel = ResultHelper.getOperationsByWellid(wellid);
                if (WarnLogModel.Count() == 0)
                    return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
                if (!string.IsNullOrEmpty(search))
                {
                    WarnLogModel = WarnLogModel.Where(u => u.isCheck.Contains(search) || u.IDCode.Contains(search) || u.OperatorID.Contains(search)).ToList();
                }
                WarnLogModel = sortWarnLogModel(sortkey, WarnLogModel);
                return Json(new { status = "0", totalnum = WarnLogModel.Count(), data = WarnLogModel.Skip((page - 1) * size).Take(size) }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "2" }, JsonRequestBehavior.AllowGet);
            }
        }
        //获取当前区块下所拥有井ID字符串
        public string getBlockWell(string blockid)
        {
            string blockwellstr = "";
            string wellstr = Session["wellstr"].ToString();
            WellService WellService = new WellService();
            var wells = WellService.All().Where(c => c.BLOCK_ID.ToString() == blockid && wellstr.Contains("-" + c.ID.ToString() + "-"));
            blockwellstr += "-";
            foreach (var well in wells)
            {
                blockwellstr += well.ID;
                blockwellstr += "-";
            }
            return blockwellstr;
        }
        //排序WarnLogModel
        public List<WarnLogModel> sortWarnLogModel(string sortkey, List<WarnLogModel> WarnLogModel)
        {
            switch (sortkey)
            {
                case "wellid desc":
                    WarnLogModel = WarnLogModel.OrderByDescending(w => w.WELL_ID).ToList();
                    break;
                case "wellid asc":
                    WarnLogModel = WarnLogModel.OrderBy(w => w.WELL_ID).ToList();
                    break;
                case "starttime desc":
                    WarnLogModel = WarnLogModel.OrderByDescending(w => w.ActionTime).ToList();
                    break;
                case "starttime asc":
                    WarnLogModel = WarnLogModel.OrderBy(w => w.ActionTime).ToList();
                    break;
                case "endtime desc":
                    WarnLogModel = WarnLogModel.OrderByDescending(w => w.ErrorEndTime).ToList();
                    break;
                case "endtime asc":
                    WarnLogModel = WarnLogModel.OrderBy(w => w.ErrorEndTime).ToList();
                    break;
                case "info desc":
                    WarnLogModel = WarnLogModel.OrderByDescending(w => w.IDCode).ToList();
                    break;
                case "info asc":
                    WarnLogModel = WarnLogModel.OrderBy(w => w.IDCode).ToList();
                    break;
                case "status desc":
                    WarnLogModel = WarnLogModel.OrderByDescending(w => w.isCheck).ToList();
                    break;
                case "status asc":
                    WarnLogModel = WarnLogModel.OrderBy(w => w.isCheck).ToList();
                    break;
                case "operator desc":
                    WarnLogModel = WarnLogModel.OrderByDescending(w => w.OperatorID).ToList();
                    break;
                case "operator asc":
                    WarnLogModel = WarnLogModel.OrderBy(w => w.OperatorID).ToList();
                    break;
                default:
                    break;
            }
            return WarnLogModel;
        }
        //排序ErrorRecord
        public List<ErrorRecord> sortErrorRecord(string sortkey, List<ErrorRecord> ErrorRecord)
        {
            switch (sortkey)
            {
                case "ID desc":
                    ErrorRecord = ErrorRecord.OrderByDescending(w => w.ID).ToList();
                    break;
                case "ID asc":
                    ErrorRecord = ErrorRecord.OrderBy(w => w.ID).ToList();
                    break;
                case "ActionTime desc":
                    ErrorRecord = ErrorRecord.OrderByDescending(w => w.ActionTime).ToList();
                    break;
                case "ActionTime asc":
                    ErrorRecord = ErrorRecord.OrderBy(w => w.ActionTime).ToList();
                    break;
                case "ErrorEndTime desc":
                    ErrorRecord = ErrorRecord.OrderByDescending(w => w.ErrorEndTime).ToList();
                    break;
                case "ErrorEndTime asc":
                    ErrorRecord = ErrorRecord.OrderBy(w => w.ErrorEndTime).ToList();
                    break;
                case "WELL_ID desc":
                    ErrorRecord = ErrorRecord.OrderByDescending(w => w.WELL_ID).ToList();
                    break;
                case "WELL_ID asc":
                    ErrorRecord = ErrorRecord.OrderBy(w => w.WELL_ID).ToList();
                    break;
                case "Describe desc":
                    ErrorRecord = ErrorRecord.OrderByDescending(w => w.Describe).ToList();
                    break;
                case "Describe asc":
                    ErrorRecord = ErrorRecord.OrderBy(w => w.Describe).ToList();
                    break;
                case "YorN desc":
                    ErrorRecord = ErrorRecord.OrderByDescending(w => w.YorN).ToList();
                    break;
                case "YorN asc":
                    ErrorRecord = ErrorRecord.OrderBy(w => w.YorN).ToList();
                    break;
                default:
                    ErrorRecord = ErrorRecord.OrderByDescending(w => w.ActionTime).ToList();
                    break;
            }
            return ErrorRecord;
        }
        //排序Record
        public List<Record> sortRecord(string sortkey, List<Record> Record)
        {
            switch (sortkey)
            {
                case "ActionTime desc":
                    Record = Record.OrderByDescending(w => w.ActionTime).ToList();
                    break;
                case "ActionTime asc":
                    Record = Record.OrderBy(w => w.ActionTime).ToList();
                    break;
                case "Describe desc":
                    Record = Record.OrderByDescending(w => w.Describe).ToList();
                    break;
                case "Describe asc":
                    Record = Record.OrderBy(w => w.Describe).ToList();
                    break;
                case "Status_Before desc":
                    Record = Record.OrderByDescending(w => w.Status_Before).ToList();
                    break;
                case "Status_Before asc":
                    Record = Record.OrderBy(w => w.Status_Before).ToList();
                    break;
                case "Status_Now desc":
                    Record = Record.OrderByDescending(w => w.Status_Now).ToList();
                    break;
                case "Status_Now asc":
                    Record = Record.OrderBy(w => w.Status_Now).ToList();
                    break;
                case "OperatorID desc":
                    Record = Record.OrderByDescending(w => w.OperatorID).ToList();
                    break;
                case "OperatorID asc":
                    Record = Record.OrderBy(w => w.OperatorID).ToList();
                    break;
                case "ControlType desc":
                    Record = Record.OrderByDescending(w => w.ControlType).ToList();
                    break;
                case "ControlType asc":
                    Record = Record.OrderBy(w => w.ControlType).ToList();
                    break;
                default:
                    Record = Record.OrderByDescending(w => w.ActionTime).ToList();
                    break;
            }
            return Record;
        }
        public ActionResult WellWarnLog()
        {
            return View();
        }
        public ActionResult WellOperationLog()
        {
            return View();
        }
        public ActionResult BlockWarnLog()
        {
            return View();
        }
        public ActionResult Get([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestModel, string blockid)
        {
            int num = 5;
            string blockwellstr = "";
            WellService WellService = new WellService();
            var wells = WellService.All();
            List<ErrorRecord> query = new List<ErrorRecord>();
            ErrorRecordService ErrorRecordService = new ErrorRecordService();
            if (Session["identity"].ToString() == "Admin")
            {
                if (blockid != "0" && blockid != null && blockid != "undefined")
                {
                    wells = wells.Where(c => c.BLOCK_ID.ToString() == blockid);
                }
            }
            else
            {
                if (blockid == "0" || blockid == null)
                {
                    string wellstr = Session["wellstr"].ToString();
                    wells = wells.Where(c => wellstr.Contains("-" + c.ID.ToString() + "-"));
                }
                else
                {
                    blockwellstr = getBlockWell(blockid);
                    wells = wells.Where(c => blockwellstr.Contains("-" + c.ID.ToString() + "-"));
                }
            }
            foreach (var well in wells)
            {
                var riers = ErrorRecordService.FindAll(c => c.WELL_ID == well.ID.ToString()).OrderByDescending(c => c.ActionTime).Take(num).ToList();
                query = query.Union(riers).ToList();
            }
            var totalcount = query.Count();

            #region Filtering
            if (requestModel.Search.Value != string.Empty)
            {
                var value = requestModel.Search.Value.Trim();
                query = query.Where(p => p.ID == Convert.ToInt32(value) ||
                                         p.WELL_ID.Contains(value) ||
                                         p.Describe.Contains(value) ||
                                         p.YorN.Contains(value)
                                    ).ToList();
            }
            var filteredCount = query.Count();
            #endregion

            #region Sorting
            var sortedColumns = requestModel.Columns.GetSortedColumns();
            var orderByString = string.Empty;
            foreach (var column in sortedColumns)
            {
                orderByString += orderByString != string.Empty ? "," : "";
                orderByString += (column.Data) + (column.SortDirection == Column.OrderDirection.Ascendant ? " asc" : " desc");
            }
            query = sortErrorRecord(orderByString, query);
            #endregion
            //Paging
            query = query.Skip(requestModel.Start).Take(requestModel.Length).ToList();

            var data = query.Select(asset => new
            {
                ID = asset.ID,
                WELL_ID = asset.WELL_ID,
                //ActionTime = asset.ActionTime.ToString("yyyy-MM-dd HH:mm:ss"),
                ActionTime = asset.ActionTime.ToString("MM/dd/yyyy HH:mm:ss"),
                Describe = asset.Describe,
                //ErrorEndTime = asset.ErrorEndTime == null ? "" : asset.ErrorEndTime.Value.ToString("yyyy-MM-dd HH:mm:ss"),
                ErrorEndTime = asset.ErrorEndTime == null ? "" : asset.ErrorEndTime.Value.ToString("MM/dd/yyyy HH:mm:ss"),
                YorN = asset.YorN
            }).ToList();

            return Json(new DataTablesResponse(requestModel.Draw, data, filteredCount, totalcount), JsonRequestBehavior.AllowGet);
        }
        public ActionResult GetWarn([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestModel, string wellid)
        {
            List<ErrorRecord> query = new List<ErrorRecord>();
            ErrorRecordService ErrorRecordService = new ErrorRecordService();
            string wellstr = Session["wellstr"].ToString();
            if (wellstr == "0" || wellstr.Contains("-" + wellid + "-"))
            {
                query = ErrorRecordService.FindAll(c => c.WELL_ID == wellid.ToString()).OrderByDescending(c => c.ActionTime).ToList();
            }
            var totalcount = query.Count();

            #region Filtering
            if (requestModel.Search.Value != string.Empty)
            {
                var value = requestModel.Search.Value.Trim();
                query = query.Where(p => p.ID == Convert.ToInt32(value) ||
                                         p.WELL_ID.Contains(value) ||
                                         p.Describe.Contains(value) ||
                                         p.YorN.Contains(value)
                                    ).ToList();
            }
            var filteredCount = query.Count();
            #endregion

            #region Sorting
            var sortedColumns = requestModel.Columns.GetSortedColumns();
            var orderByString = string.Empty;
            foreach (var column in sortedColumns)
            {
                orderByString += orderByString != string.Empty ? "," : "";
                orderByString += (column.Data) + (column.SortDirection == Column.OrderDirection.Ascendant ? " asc" : " desc");
            }
            query = sortErrorRecord(orderByString, query);
            #endregion
            //Paging
            query = query.Skip(requestModel.Start).Take(requestModel.Length).ToList();

            var data = query.Select(asset => new
            {
                ID = asset.ID,
                WELL_ID = asset.WELL_ID,
                ActionTime = asset.ActionTime.ToString("yyyy-MM-dd HH:mm:ss"),
                Describe = asset.Describe,
                ErrorEndTime = asset.ErrorEndTime == null ? "" : asset.ErrorEndTime.Value.ToString("yyyy-MM-dd HH:mm:ss"),
                YorN = asset.YorN
            }).ToList();

            return Json(new DataTablesResponse(requestModel.Draw, data, filteredCount, totalcount), JsonRequestBehavior.AllowGet);
        }
        public ActionResult GetOperator([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestModel, string wellid)
        {
            List<Record> query = new List<Record>();
            RecordService ErrorRecordService = new RecordService();
            string wellstr = Session["wellstr"].ToString();
            if (wellstr == "0" || wellstr.Contains("-" + wellid + "-"))
            {
                query = ErrorRecordService.FindAll(c => c.WELL_ID == wellid.ToString()).OrderByDescending(c => c.ActionTime).ToList();
            }
            var totalcount = query.Count();

            #region Filtering
            if (requestModel.Search.Value != string.Empty)
            {
                var value = requestModel.Search.Value.Trim();
                query = query.Where(p => p.OperatorID.Contains(value) ||
                                         p.WELL_ID.Contains(value) ||
                                         p.Describe.Contains(value) ||
                                         p.ControlType.Contains(value)
                                    ).ToList();
            }
            var filteredCount = query.Count();
            #endregion

            #region Sorting
            var sortedColumns = requestModel.Columns.GetSortedColumns();
            var orderByString = string.Empty;
            foreach (var column in sortedColumns)
            {
                orderByString += orderByString != string.Empty ? "," : "";
                orderByString += (column.Data) + (column.SortDirection == Column.OrderDirection.Ascendant ? " asc" : " desc");
            }
            query = sortRecord(orderByString, query);
            #endregion
            //Paging
            query = query.Skip(requestModel.Start).Take(requestModel.Length).ToList();
            R_ADJU_StructureService R_ADJU_StructureService = new R_ADJU_StructureService();
            var rasss = R_ADJU_StructureService.All().ToList();
            AspNetUsersService AspNetUsersService = new AspNetUsersService();
            var user = AspNetUsersService.All().ToList();
            var data = query.Select(asset => new
            {
                ActionTime = asset.ActionTime.ToString("yyyy-MM-dd HH:mm:ss"),
                Describe = asset.Describe,
                Status_Before = asset.Status_Before,
                Status_Now = (Convert.ToDouble(asset.Status_Now) * (rasss.Find(c => c.Describe.Trim() == asset.Describe)).Scaler).ToString() + (rasss.Find(c => c.Describe.Trim() == asset.Describe)).Unit,
                OperatorID = (user.Find(c => c.Email.Trim() == asset.OperatorID)).UserName,//asset.OperatorID,
                ControlType = asset.ControlType
            }).ToList();

            return Json(new DataTablesResponse(requestModel.Draw, data, filteredCount, totalcount), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取某个人管理的井的报警信息
        /// </summary>
        /// <returns></returns>
        public JsonResult GetOnesWarn()
        {
            R_INFO_Error_RecordeService R_INFO_Error_RecordeService = new BLL.R_INFO_Error_RecordeService();
            R_INFO_StructureService R_INFO_StructureService = new R_INFO_StructureService();
            User2WellService User2WellService = new User2WellService();
            var rs = R_INFO_Error_RecordeService.All().GroupBy(c => c.WELL_ID);
            string wellstr = Session["wellstr"].ToString();
            string userid = Session["userid"].ToString();
            IQueryable<User2Well> user2well = User2WellService.FindAll(c => c.UserId == userid);
            List<WarnLogModel> wlml = new List<WarnLogModel>();
            foreach (var rss in rs)
            {
                var rsss = rss.GroupBy(c => c.IDCode).ToList();
                int wellid = int.Parse(rss.ToList().FirstOrDefault().WELL_ID);
               if (wellstr == "0" || wellstr.Contains("-" + wellid + "-"))
                {
                    string notIgnore = user2well.Where(c => c.Well.ID == wellid).ToList().FirstOrDefault().NotIgnore;
                    foreach (var rssss in rsss)
                    {
                        var R_INFO_Error_RecordeList = rssss.OrderByDescending(c => c.ActionTime).ToList();
                        if (R_INFO_Error_RecordeList.Count > 0)
                        {
                            var rs1 = R_INFO_Error_RecordeList[0];
                            WarnLogModel wlm = new WarnLogModel();
                            if (notIgnore.Contains(rs1.IDCode.Trim()) && (rs1.isCheck == null || rs1.isCheck == "0"))
                            {
                                wlm.RecordeID = rs1.ID;
                                wlm.WELL_ID = rs1.WELL_ID;
                                wlm.OperatorID = rs1.OperatorID;
                                wlm.ActionTime = rs1.ActionTime.ToString();
                                wlm.IDCode = R_INFO_StructureService.Find(c => c.IDCode == rs1.IDCode).Describe;
                                wlm.Parameter = rs1.Parameter;
                                wlm.Status_Before = rs1.Status_Before;
                                wlm.Status_Now = rs1.Status_Now;
                                wlm.ErrorEndTime = rs1.ErrorEndTime.ToString();
                                wlm.isCheck = rs1.isCheck;
                                wlml.Add(wlm);
                            }
                        }
                    }
                }
            }
            //foreach(var rs1 in rs)
            //{
            //    WarnLogModel wlm = new WarnLogModel();
            //    if (wellstr == "0" || wellstr.Contains("-" + rs1.WELL_ID + "-"))
            //    {
            //        if(rs1.isCheck == null || rs1.isCheck == "0")
            //        {
            //            wlm.RecordeID = rs1.ID;
            //            wlm.WELL_ID = rs1.WELL_ID;
            //            wlm.OperatorID = rs1.OperatorID;
            //            wlm.ActionTime = rs1.ActionTime.ToString();
            //            wlm.IDCode = rs1.IDCode;
            //            wlm.Parameter = rs1.Parameter;
            //            wlm.Status_Before = rs1.Status_Before;
            //            wlm.Status_Now = rs1.Status_Now;
            //            wlm.ErrorEndTime = rs1.ErrorEndTime.ToString();
            //            wlm.isCheck = rs1.isCheck;
            //            wlml.Add(wlm);
            //        }
            //    }
            //}
            if (wlml.Count > 0)
            {
                return Json(new { status = "0", data = wlml }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
            }
        }
        /// <summary>
        /// 忽略某个井的报警信息
        /// </summary>
        /// <param name="wellid"></param>
        /// <returns></returns>
        public JsonResult IgnoreWarn(string wellid)
        {
            int id = int.Parse(wellid);
            R_INFO_Error_RecordeService R_INFO_Error_RecordeService = new BLL.R_INFO_Error_RecordeService();
            R_INFO_Error_Recorde RIER = R_INFO_Error_RecordeService.Find(c => c.ID == id);
            RIER.isCheck = "1";
            try
            {
                R_INFO_Error_RecordeService.Update(RIER);
            }
            catch (Exception e)
            {
                return Json(new { status = "7", message = e }, JsonRequestBehavior.AllowGet);
            }
            return Json(new { status = "0" }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetOnesWorker(string wellid)
        {
            int id = int.Parse(wellid);
            Worker2WellService w2ws = new Worker2WellService();
            WorkerService ws = new WorkerService();
            var w2wlist = w2ws.FindAll(c => c.Well.ID == id).ToList();
            var wslist = ws.All().ToList();
            List<global::Models.Worker> datalist = new List<global::Models.Worker>();
            foreach (Worker2Well w2w in w2wlist)
            {
                global::Models.Worker mw = new global::Models.Worker();
                mw = wslist.Find(c => c.Id == w2w.Worker.Id);
                datalist.Add(mw);
            }
            //var list = new List<string> { "井口油压", "井口温度", "井口套压", "汇管压力", "汇管温度", "有功功率", "功率因数", "平衡率", "系统效率", "井口油罐液位", "变频器输出频率", "A相电流有效值", "B相电流有效值", "C相电流有效值", "A相电压有效值", "B相电压有效值", "C相电压有效值", "油井加热炉油温", "伴热温度（抽油机井）", "伴热压力（抽油机井）", "掺稀流量（瞬时量）", "周期内A相电流最大值", "周期内B相电流最大值", "周期内C相电流最大值", "周期内总有功功率最大值", "周期内总无功功率最大值", "周期内总功率因数最大值" };
            return Json(new { status = "0", data = datalist }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取某个井的全部报警信息
        /// </summary>
        /// <returns></returns>
        public JsonResult GetWellWarn(string wellid)
        {
            R_INFO_Error_RecordeService R_INFO_Error_RecordeService = new BLL.R_INFO_Error_RecordeService();
            R_INFO_StructureService R_INFO_StructureService = new R_INFO_StructureService();
            User2WellService User2WellService = new User2WellService();
            var rs = R_INFO_Error_RecordeService.All().GroupBy(c => c.WELL_ID);
            string wellstr = Session["wellstr"].ToString();
            string userid = Session["userid"].ToString();
            IQueryable<User2Well> user2well = User2WellService.FindAll(c => c.UserId == userid);
            List<WarnLogModel> wlml = new List<WarnLogModel>();
            foreach (var rss in rs)
            {
                var rsss = rss.GroupBy(c => c.IDCode).ToList();
                int wellid2 = int.Parse(rss.ToList().FirstOrDefault().WELL_ID);
                if (wellstr == "0" || wellstr.Contains("-" + wellid2 + "-"))
                {
                    string notIgnore = user2well.Where(c => c.Well.ID == wellid2).ToList().FirstOrDefault().NotIgnore;
                    foreach (var rssss in rsss)
                    {
                        var R_INFO_Error_RecordeList = rssss.OrderByDescending(c => c.ActionTime).ToList();
                        if (R_INFO_Error_RecordeList.Count > 0)
                        {
                            var rs1 = R_INFO_Error_RecordeList[0];
                            WarnLogModel wlm = new WarnLogModel();
                            if (notIgnore.Contains(rs1.IDCode.Trim()) && rs1.WELL_ID.Trim() == wellid)
                            {
                                wlm.RecordeID = rs1.ID;
                                wlm.WELL_ID = rs1.WELL_ID;
                                wlm.OperatorID = rs1.OperatorID;
                                wlm.ActionTime = rs1.ActionTime.ToString();
                                wlm.IDCode = R_INFO_StructureService.Find(c => c.IDCode == rs1.IDCode).Describe;
                                wlm.Parameter = rs1.Parameter;
                                wlm.Status_Before = rs1.Status_Before;
                                wlm.Status_Now = rs1.Status_Now;
                                wlm.ErrorEndTime = rs1.ErrorEndTime.ToString();
                                wlm.isCheck = rs1.isCheck;
                                wlml.Add(wlm);
                            }
                        }
                    }
                }
            }
            if (wlml.Count > 0)
            {
                return Json(new { status = "0", data = wlml }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}